package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.MemberLevel;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.MemberLevelService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/07 15:35
 * 
 * 会员等级管理类
 */
@RestController
@RequestMapping("/memberLevel/")
public class MemberLevelController {
    
    @Autowired
    MemberLevelService memberLevelService;

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @param draw 前台参数，照样返回chang
     * @param sort time
     * @param way desc，asc，noway
     * @Reutrn RetResult<PageInfo<UserLabel>>
     */
    @GetMapping("/list/{page}/{size}/{draw}/{sort}/{way}")
    public Response list(@PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size,
                                    @PathVariable("draw") String draw,
                                    @PathVariable("sort") String sort,
                                    @PathVariable("way") String way){
        Response response = new Response();
        PageHelper.startPage(page, size);
        List<MemberLevel> list = memberLevelService.selectAllByDel(0, sort, way);
        PageInfo<MemberLevel> pageInfo = new PageInfo<>(list);
        if(list.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.put("pageData", pageInfo.getList());
            response.put("recordsTotal", pageInfo.getTotal());
            response.put("draw", draw);
            response.put("pages", pageInfo.getPages());
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    /**
     * 添加会员等级
     * @param memberLevel
     * @return
     */
    @PostMapping("/save")
    public Response save(MemberLevel memberLevel){
        return memberLevelService.save(memberLevel);
    }

    /**
     * 修改会员等级
     * @param memberLevel
     * @return
     */
    @PutMapping("/update")
    public Response update(MemberLevel memberLevel){
        return memberLevelService.update(memberLevel);
    }

    /**
     * 查询会员等级详情
     * @param id:会员等级的id
     * @return
     */
    @GetMapping("/getMemberLevelById/{id}")
    public Response getMemberLevelById(@PathVariable("id")  String id){
        return memberLevelService.getMemberLevelById(id);
    }

    /**
     * 删除
     * @param id:会员等级的id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id")  String id){
        return memberLevelService.deleteById(id);
    }

    /**
     * 更改会员等级的状态
     * @param id:会员等级的id
     * @param status:会员等级的状态
     * @return
     */
    @GetMapping("/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id")  String id,
                                            @PathVariable("status") Integer status){
        return memberLevelService.changeStatus(id, status);
    }

    /**
     * 批量删除
     * @param id:会员等级的id集合
     * @return
     */
    @DeleteMapping("/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id")  String[] id){
        return memberLevelService.batchDeleteById(id);
    }
}
